package com.xsomnus.java_basic.juc;

/**
 * @author 叶小夏
 * @since 2019-08-15 18:39
 */
public class Test222 {

    private static int leftTicket = 100;

    static class Sub extends Thread {

        private String id;

        public Sub(String id) {
            this.id = id;
        }

        @Override
        public void run() {
            while (true) {
                if (leftTicket > 0) {
                    leftTicket--;
                    try {
                        Thread.sleep(60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getId() + " " + id + "买到票" + leftTicket);
                } else {
                    break;
                }
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        Sub sub1 = new Sub("1");
        Sub sub2 = new Sub("2");
        Sub sub3 = new Sub("3");
        Sub sub4 = new Sub("4");


        sub1.start();
        sub2.start();
        sub3.start();
        sub4.start();
        Thread.sleep(6000);
        System.out.println(leftTicket);
    }
}
